package HiveUDF;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ysy
 * @version 1.0   2020/11/25 14:52
 */
public class DateT extends UDF {
    public Text evaluate(Text d) throws ParseException {
        /*日期转换自定义函数*/
        SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy");
        SimpleDateFormat sdt = new SimpleDateFormat("yyyy-MM-dd");
        String afterParseDate="";
        try {
            Date dT = sd.parse(d.toString());
            afterParseDate = sdt.format(dT);
        }catch (Exception e){
            System.out.println("日期转换格式发生异常："+e.toString());
        }

        return new Text(afterParseDate);
    }

    public static void main(String[] args) throws ParseException {
        DateT dateT = new DateT();
        Text input = dateT.evaluate(new Text("01/03/2022"));
        System.out.println(input);
    }
}
